package com.streamer.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.streamer.app.R
import com.streamer.app.bean.WinningRecordBean
import com.streamer.app.bean.WinningRecordGroup

class WinningRecordGroupAdapter(
    private val groups: MutableList<WinningRecordGroup>
) : RecyclerView.Adapter<WinningRecordGroupAdapter.GroupVH>() {

    class GroupVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvReleaseTime: TextView = itemView.findViewById(R.id.tv_release_time)
        val layoutRecordsContainer: LinearLayout = itemView.findViewById(R.id.layout_records_container)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GroupVH {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_winning_record_group, parent, false)
        return GroupVH(view)
    }

    override fun onBindViewHolder(holder: GroupVH, position: Int) {
        val group = groups[position]
        
        // 设置发放时间
        holder.tvReleaseTime.text = "发放时间: ${group.releaseTime}"
        
        // 清空容器
        holder.layoutRecordsContainer.removeAllViews()
        
        // 动态添加记录行
        group.records.forEach { record ->
            val recordRow = createRecordRow(holder.itemView.context, record)
            holder.layoutRecordsContainer.addView(recordRow)
        }
    }

    override fun getItemCount(): Int = groups.size

    private fun createRecordRow(context: android.content.Context, record: WinningRecordBean): View {
        val rowView = LinearLayout(context).apply {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            setBackgroundResource(R.drawable.view_white_bg)
            orientation = LinearLayout.HORIZONTAL
            gravity = android.view.Gravity.CENTER_VERTICAL
            setPadding(48, 12, 48, 12) // 左右padding比表头多，形成缩进效果
        }

        // 抽奖名称
        val tvLotteryName = TextView(context).apply {
            layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
            text = record.lotteryName
            setTextColor(context.getColor(R.color.black))
            textSize = 14f
        }

        // 昵称
        val tvWinnerName = TextView(context).apply {
            layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
            text = record.winnerName
            setTextColor(context.getColor(R.color.black))
            textSize = 14f
        }

        // 奖品名称
        val tvPrizeName = TextView(context).apply {
            layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)
            text = record.prizeName
            setTextColor(context.getColor(R.color.black))
            textSize = 14f
        }

        rowView.addView(tvLotteryName)
        rowView.addView(tvWinnerName)
        rowView.addView(tvPrizeName)

        return rowView
    }

    fun addGroup(group: WinningRecordGroup) {
        groups.add(0, group) // 添加到列表开头
        notifyItemInserted(0)
    }

    fun addGroups(newGroups: List<WinningRecordGroup>) {
        val startPosition = groups.size
        groups.addAll(newGroups)
        notifyItemRangeInserted(startPosition, newGroups.size)
    }

    fun updateGroups(newGroups: List<WinningRecordGroup>) {
        groups.clear()
        groups.addAll(newGroups)
        notifyDataSetChanged()
    }

    fun clearGroups() {
        groups.clear()
        notifyDataSetChanged()
    }
} 